package com.ymatou.iapi.shoppingcartservice.testcase;

import java.util.Map;

import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.shoppingcartservice.parameter.ShoppingcartProductBean;
import com.ymatou.iapi.shoppingcartservice.service.AddShoppingCartCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.BuyerShoppingCartService;
import com.ymttest.database.mongo.YmtShoppingCartWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.MapUtil;

public class Ts_AddShoppingCart {
	
	private static AddShoppingCartCall addShoppingCartCall = new AddShoppingCartCall();
	private static Integer userId = Integer.valueOf(EnvSetup.getData("shoppingcartUserId"));
	YmtShoppingCartWapper ymtShoppingCartWapper = new YmtShoppingCartWapper();
	BuyerShoppingCartService buyerShoppingCartService = new BuyerShoppingCartService();
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("添加商品");
	}
	
	@Before
	public void caseUp() {
	}
	
	@AfterClass
	public static void tearDown() {
		Logger.generateResult(addShoppingCartCall.getOpurl());
	}	
	
	@After
	public void caseDown() {
		Logger.end();
		try {
			ymtShoppingCartWapper.deleteByUserId(userId);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_AddShoppingCart_001() throws Exception {
		Logger.start(true,"购物车添加单个商品");
		try{
			ShoppingcartProductBean shoppingcartProductBean = ShoppingcartProductBean.getAddShoppingCartRequestFromFile("addshoppingcartcalltpl", 
					MapUtil.hashMap("userId", userId));
			
			JSONObject ret = addShoppingCartCall.callAndGetReturnData(shoppingcartProductBean);
			int code = ret.getInt("code");
			
			Logger.verifyEquals(200, code, "接口code返回异常");
			buyerShoppingCartService.checkShoppingcartProductBeanFromDB(shoppingcartProductBean);
			
		}catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_AddShoppingCart_002() throws Exception {
		Logger.start(true,"购物车添加已经存在的商品");
		try{
			ShoppingcartProductBean shoppingcartProductBean = ShoppingcartProductBean.getAddShoppingCartRequestFromFile("addshoppingcartcalltpl", 
					MapUtil.hashMap("userId", userId, "num", 19));
			
			Map<String,Object> shoppingcartProductDB = ymtShoppingCartWapper.
					getByCatalogIdIdAndUserId(shoppingcartProductBean.getCatalogId(), userId);
			
			while ( null == shoppingcartProductDB) {
				addShoppingCartCall.callAndGetReturnData(ShoppingcartProductBean.getAddShoppingCartRequestFromFile("addshoppingcartcalltpl", 
						MapUtil.hashMap("userId", userId)));
				shoppingcartProductDB = ymtShoppingCartWapper.
						getByCatalogIdIdAndUserId(shoppingcartProductBean.getCatalogId(), userId);
			}
			
			JSONObject ret = addShoppingCartCall.callAndGetReturnData(shoppingcartProductBean);
			shoppingcartProductDB = ymtShoppingCartWapper.
					getByCatalogIdIdAndUserId(shoppingcartProductBean.getCatalogId(), userId);
			int code = ret.getInt("code");
			int addAfter = Integer.parseInt(shoppingcartProductDB.get("num").toString());
			
			Logger.verifyEquals(200, code, "接口code返回异常");
			Logger.verifyEquals(shoppingcartProductBean.getNum(), addAfter, "购物车数量不正确");
		}catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_AddShoppingCart_003() throws Exception {
		Logger.start(true,"传入错误的sellerid");
		try{
			ShoppingcartProductBean shoppingcartProductBean = ShoppingcartProductBean.getAddShoppingCartRequestFromFile("addshoppingcartcalltpl", 
					MapUtil.hashMap("userId", userId, "sellerId", 0));
			
			JSONObject ret = addShoppingCartCall.callAndGetReturnData(shoppingcartProductBean);
			int code = ret.getInt("code");
			String message = ret.getString("message");
			
			Logger.verifyEquals(500, code, "接口code返回异常");
			Logger.verifyEquals("sellerId:错误的sellerId;", message, "sellerid错误提示不正确");

		}catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_AddShoppingCart_004() throws Exception {
		Logger.start(true,"传入错误的userId");
		try{
			ShoppingcartProductBean shoppingcartProductBean = ShoppingcartProductBean.getAddShoppingCartRequestFromFile("addshoppingcartcalltpl", 
					MapUtil.hashMap("userId", 0));
			
			JSONObject ret = addShoppingCartCall.callAndGetReturnData(shoppingcartProductBean);
			int code = ret.getInt("code");
			String message = ret.getString("message");
			
			Logger.verifyEquals(500, code, "接口code返回异常");
			Logger.verifyEquals("userId:错误的userId;", message, "userid错误提示不正确");
		}catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_AddShoppingCart_005() throws Exception {
		Logger.start(true,"传入错误的num");
		try{
			ShoppingcartProductBean shoppingcartProductBean = ShoppingcartProductBean.getAddShoppingCartRequestFromFile("addshoppingcartcalltpl", 
					MapUtil.hashMap("userId", userId, "num", 0));
			
			JSONObject ret = addShoppingCartCall.callAndGetReturnData(shoppingcartProductBean);
			int code = ret.getInt("code");
			String message = ret.getString("message");
			
			Logger.verifyEquals(500, code, "接口code返回异常");
			Logger.verifyEquals("num:错误的商品数量;", message, "num错误提示不正确");
		}catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_AddShoppingCart_006() throws Exception {
		Logger.start(true,"传入错误的catalogId");
		try{
			ShoppingcartProductBean shoppingcartProductBean = ShoppingcartProductBean.getAddShoppingCartRequestFromFile("addshoppingcartcalltpl", 
					MapUtil.hashMap("userId", userId, "catalogId", ""));
			
			JSONObject ret = addShoppingCartCall.callAndGetReturnData(shoppingcartProductBean);
			int code = ret.getInt("code");
			String message = ret.getString("message");
			
			Logger.verifyEquals(500, code, "接口code返回异常");
			Logger.verifyEquals("catalogId:规格不能为空;", message, "catalogId错误提示不正确");
		}catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_AddShoppingCart_007() throws Exception {
		Logger.start(true,"传入错误的productId");
		try{
			ShoppingcartProductBean shoppingcartProductBean = ShoppingcartProductBean.getAddShoppingCartRequestFromFile("addshoppingcartcalltpl", 
					MapUtil.hashMap("userId", userId, "productId", ""));
			
			JSONObject ret = addShoppingCartCall.callAndGetReturnData(shoppingcartProductBean);
			int code = ret.getInt("code");
			String message = ret.getString("message");
			
			Logger.verifyEquals(500, code, "接口code返回异常");
			Logger.verifyEquals("productId:商品id不能为空;", message, "productId错误提示不正确");
		}catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_AddShoppingCart_008() throws Exception {
		Logger.start(true,"可添加成功最少参数验证");
		try{
			ShoppingcartProductBean shoppingcartProductBean = new ShoppingcartProductBean();
			
			shoppingcartProductBean.setUserId(55421);
			shoppingcartProductBean.setSellerId(44213);
			shoppingcartProductBean.setCatalogId("catalogId-min");
			shoppingcartProductBean.setProductId("productId-min");
			shoppingcartProductBean.setNum(5);
			
			JSONObject ret = addShoppingCartCall.callAndGetReturnData(shoppingcartProductBean);
			int code = ret.getInt("code");
			
			Map<String,Object> shoppingcartProductDB = ymtShoppingCartWapper.
					getByCatalogIdIdAndUserId(shoppingcartProductBean.getCatalogId(), 55421);
			
			Logger.verifyEquals(200, code, "接口code返回异常");
			Logger.verifyEquals(shoppingcartProductBean.getUserId(), Integer.parseInt(shoppingcartProductDB.get("userid").toString()), "userId错误");
			Logger.verifyEquals(shoppingcartProductBean.getSellerId(), Integer.parseInt(shoppingcartProductDB.get("sellerid").toString()), "sellerId错误");
			Logger.verifyEquals(shoppingcartProductBean.getCatalogId(), shoppingcartProductDB.get("catalogid").toString(), "catalogId错误");
			Logger.verifyEquals(shoppingcartProductBean.getProductId(), shoppingcartProductDB.get("productid").toString(), "productId错误");
			Logger.verifyEquals(shoppingcartProductBean.getNum(), Integer.parseInt(shoppingcartProductDB.get("num").toString()), "num错误");

		}catch (Exception e) {
			Logger.fail(e);
		}
	}
}
